package com.hz.ruoyilgorder.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.hz.rouyigonggong.domain.LgDriverLogistics;

import com.hz.rouyigonggong.domain.LgInboundOrder;
import com.hz.rouyigonggong.domain.LgSellerOrder;
import com.hz.rouyigonggong.domain.basedomain.BaseDomain;
import com.hz.rouyigonggong.domain.dto.LgDriverLogisticsDTO;


import com.hz.ruoyilgorder.config.LgDriverLogisticsConverter;

import com.hz.ruoyilgorder.service.LgDriverLogisticsService;
import com.hz.ruoyilgorder.service.LgInboundOrderService;
import com.hz.ruoyilgorder.service.LgSellerOrderService;
import com.ruoyi.common.core.constant.HttpStatus;
import com.ruoyi.common.core.utils.PageUtils;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @version 1.0
 * @Author yzw
 * @Date 2025/3/6 16:28
 * @注释 司机接单
 */
@RestController
@RequestMapping("/goorder")
public class LgDriverLogisticsController {
    //司机接单
    @Autowired
    private LgDriverLogisticsService lgDriverLogisticsService;
    //入库订单
    @Autowired
    private LgInboundOrderService lgInboundOrderService;
    //卖家下单
    @Autowired
    private LgSellerOrderService lgSellerOrderService;

    // 获取司机接单信息 查询全部
    @GetMapping("/getList")
    public TableDataInfo getList(LgDriverLogisticsDTO entity){
        startPage();
        entity.setCreateBy(String.valueOf(BaseDomain.getUserId()));
        List<LgDriverLogisticsDTO> list = lgDriverLogisticsService.getList(entity);
        return  getDataTable(list);

    }

    @RequiresPermissions("goorder:every:query")
    @GetMapping(value = "/getInfo/{id}")
    public AjaxResult getInfo(@PathVariable Integer id) {

        return AjaxResult.success(lgDriverLogisticsService.getInfo(id));
    }


    @RequiresPermissions("goorder:every:edit")
    @PostMapping("/edit")
    @Transactional
    public AjaxResult edit(@Validated @RequestBody LgDriverLogisticsDTO orderDTO) {
        orderDTO.setCreateBy(String.valueOf(BaseDomain.getUserId()));
        //修改卖家下单状态为取货中
       if(orderDTO.getStatus()==1) {
           Wrapper<LgSellerOrder> lgSellerOrderWrapper = new QueryWrapper<LgSellerOrder>()
                   .eq("order_number",orderDTO.getLogisticsOrderNumber());
           // 创建一个更新的实体对象，只更新状态字段
           LgSellerOrder updatedOrder = new LgSellerOrder();
           updatedOrder.setDepositPaid(5);
           lgSellerOrderService.update(updatedOrder,lgSellerOrderWrapper);
       }
        //修改卖家下单状态为取货中
        if(orderDTO.getStatus()==4) {
            Wrapper<LgSellerOrder> lgSellerOrderWrapper = new QueryWrapper<LgSellerOrder>()
                    .eq("order_number",orderDTO.getLogisticsOrderNumber());
            // 创建一个更新的实体对象，只更新状态字段
            LgSellerOrder updatedOrder = new LgSellerOrder();
            updatedOrder.setDepositPaid(6);
            lgSellerOrderService.update(updatedOrder,lgSellerOrderWrapper);
        }

        //状态等于5时，修改司机接单表状态为已完成，插入入库表数据
        if (orderDTO.getStatus() == 5) {
            LgInboundOrder lg = new LgInboundOrder();
            lg.setOrderNumber(orderDTO.getLogisticsOrderNumber());
            //插入管理员id
            lg.setCreateBy(String.valueOf(BaseDomain.getUserId()));
            lg.setSellerId(orderDTO.getSellerId());
            lg.setGoodsId(orderDTO.getGoodsId());
            lg.setUnitId(orderDTO.getUnit());
            lg.setSubId(orderDTO.getSubId());
            lg.setStatus(0);
            //先修改
//            LgDriverLogistics entity = LgDriverLogisticsConverter.toEntity(orderDTO);
//            boolean b = lgDriverLogisticsService.updateById(entity);
//            if (b) {
                //再新增
                lgInboundOrderService.save(lg);
//            }
//         //修改卖家状态为 待支付余款
//            Wrapper<LgSellerOrder> lgSellerOrderWrapper = new QueryWrapper<LgSellerOrder>()
//                    .eq("order_number",orderDTO.getLogisticsOrderNumber());
//            // 创建一个更新的实体对象，只更新状态字段
//            LgSellerOrder updatedOrder = new LgSellerOrder();
//            updatedOrder.setDepositPaid(6);
//            lgSellerOrderService.update(updatedOrder,lgSellerOrderWrapper);
        }
        //修改数据
        LgDriverLogistics entity = LgDriverLogisticsConverter.toEntity(orderDTO);
        Integer logisticsOrderNumber = entity.getLogisticsOrderNumber();
        Wrapper<LgDriverLogistics> lgSellerOrderWrapper = new QueryWrapper<LgDriverLogistics>()
                .eq("logistics_order_number", logisticsOrderNumber);
        return AjaxResult.success( lgDriverLogisticsService.update(entity, lgSellerOrderWrapper));
    }

    @RequiresPermissions("goorder:every:remove")
    @GetMapping("/remove/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {

        return AjaxResult.success(lgDriverLogisticsService.removeByIds(Arrays.asList(ids)));
    }


    protected void startPage()
    {
        PageUtils.startPage();
    }
    protected TableDataInfo getDataTable(List<?> list)
    {
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setRows(list);
        rspData.setMsg("查询成功");
        rspData.setTotal(new PageInfo(list).getTotal());
        return rspData;
    }

}
